﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Xml;

namespace FlickrPortfolio.Controllers {
    public class SetupController : Controller {
        public ActionResult Index() {
            return RedirectToAction("Authorise");
        }

        public ActionResult Authorise() {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Authorise(FormCollection form) {
            var flickr = new DataAccess.Flickr();
            Authorise(flickr.GetToken(form["miniToken"]));
            return RedirectToAction("Index", "Home");
        }

        private void Authorise(FlickrNet.Auth token) {
            var config = new XmlDocument();
            config.Load("D:\\Documents\\Visual Studio 2008\\Projects\\FlickrPortfolio\\FlickrPortfolio\\App_Data\\Config.xml");

            config.InnerXml = config.InnerXml.Replace("<Token></Token>", "<Token>" + token.Token + "</Token>");
            config.InnerXml = config.InnerXml.Replace("<User></User>", "<User>" + token.User.UserId + "</User>");

            //if (tokenNode != null)
            //    tokenNode.Value = token.Token;

            //if (userNode != null)
            //    userNode.Value = token.User.UserId;

            config.Save("D:\\Documents\\Visual Studio 2008\\Projects\\FlickrPortfolio\\FlickrPortfolio\\App_Data\\Config.xml");
        }
    }
}
